package com.example.bilibili_fake.data.network

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import com.google.gson.GsonBuilder
import com.google.gson.Strictness

object ApiClient {
    private const val BASE_URL = "https://api.bilibili.com/"

    private val loggingInterceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }

    private val okHttpClient = OkHttpClient.Builder()
        .addInterceptor(loggingInterceptor)
        .connectTimeout(10, TimeUnit.SECONDS)
        .readTimeout(10, TimeUnit.SECONDS)
        .build()

    private val bannerOkHttpClient = OkHttpClient.Builder()
        .addInterceptor { chain ->
            val original = chain.request()
            val requestBuilder = original.newBuilder()
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
                .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .header("Priority", "u=0, i")
                .header("sec-ch-ua", "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Microsoft Edge\";v=\"140\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("sec-fetch-dest", "document")
                .header("sec-fetch-mode", "navigate")
                .header("sec-fetch-site", "none")
                .header("sec-fetch-user", "?1")
                .header("upgrade-insecure-requests", "1")
                .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0")
                .header("referer", "https://www.bilibili.com/")
                // 注意：实际应用中Cookie应该是动态获取的，这里为了演示写死
                .header("Cookie", "buvid3=35A866D7-97D0-99E7-9BDF-B80AB9ED72DE87194infoc; b_nut=1752218587; _uuid=91923983-92C5-D10E2-A3CD-75A1029C810EAE88060infoc; buvid_fp=0403c80450a0d6e95d68a88b7472d45a; buvid4=90DE283C-1E31-A448-8678-27AD4356A02389301-025071115-jGyCwRPGlb0Pk7FUa3lEVw%3D%3D; header_theme_version=OPEN; enable_web_push=DISABLE; rpdid=|(JYYJY|kkl)0J'u~lkku)Yu~; CURRENT_FNVAL=4048; CURRENT_QUALITY=0; theme-tip-show=SHOWED; bp_t_offset_75893179=1115330756831346688; theme-avatar-tip-show=SHOWED; home_feed_column=4; browser_resolution=150-962; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTkwNDAxODUsImlhdCI6MTc1ODc4MDkyNSwicGx0IjotMX0.cG8wuu1bwT52vALrHT7lVinwe6dsldXYRaendcWjWYY; bili_ticket_expires=1759040125; theme-switch-show=SHOWED")
                .header("content-type", "application/json; charset=utf-8")
                .method(original.method, original.body)
            val request = requestBuilder.build()
            chain.proceed(request)
        }
        .build()

    val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val bannerRetrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(bannerOkHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val apiService: SplashApi = retrofit.create(SplashApi::class.java)
    val videoApiService: VideoApi = retrofit.create(VideoApi::class.java)
    val bannerApi: BannerApi = bannerRetrofit.create(BannerApi::class.java)
}